﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CommonUtil
{
    public class EncryptTools
    {
        /// <summary>
        /// 字符串MD5加密
        /// </summary>
        /// <param name="ostr"></param>
        /// <returns></returns>
        public static string EncryptMD5(string ostr)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(ostr));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }


        #region AES加解密

        /// <summary>
        /// AES密钥（32位）
        /// </summary>
        public const string m_AES_Key = "b707ca10c6b94d309ac76c729165c981";

        /// <summary>
        /// AES加密矢量(16位)
        /// </summary>
        public const string m_AES_Vector = "73a692e5e9db2ec2";

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptStr">明文</param>
        /// <returns></returns>
        public static string EncryptAES(string encryptStr)
        {
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);//获得以UTF8编码格式的子节数组

            RijndaelManaged rm = new RijndaelManaged();//设定加密器
            rm.Key = UTF8Encoding.UTF8.GetBytes(m_AES_Key);//设置加密的密钥
            rm.IV = UTF8Encoding.UTF8.GetBytes(m_AES_Vector);//设置加密的矢量
            rm.Mode = CipherMode.ECB;//设置加密的运算模式
            rm.Padding = PaddingMode.PKCS7;//设置置加密算法的填充模式
            ICryptoTransform cTransform = rm.CreateEncryptor();//建立加密器

            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <returns></returns>
        public static string DecryptAES(string decryptStr)
        {
            byte[] toEncryptArray = Convert.FromBase64String(decryptStr);

            RijndaelManaged rm = new RijndaelManaged();//设定加密器
            rm.Key = UTF8Encoding.UTF8.GetBytes(m_AES_Key);//设置加密的密钥
            rm.IV = UTF8Encoding.UTF8.GetBytes(m_AES_Vector);//设置加密的矢量
            rm.Mode = CipherMode.ECB;//设置加密的运算模式
            rm.Padding = PaddingMode.PKCS7;//设置置加密算法的填充模式
            ICryptoTransform cTransform = rm.CreateDecryptor();//建立解密器

            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        #endregion


        #region Base64加解密

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="context">需要加密的字符串</param>
        /// <returns>返回加密的结果</returns>
        public static string EncryptionBase64(string context)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(context));
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string DecryptionBase64(string context)
        {
            return Encoding.UTF8.GetString(Convert.FromBase64String(context));
        }

        #endregion


        #region SHA256加密

        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <returns></returns>
        public static string EncryptSHA256(string encryptStr)
        {
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);//获得以UTF8编码格式的子节数组
            byte[] resultArray = new SHA256Managed().ComputeHash(toEncryptArray);
            return BitConverter.ToString(resultArray).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// 软素特色的SHA256加密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <returns></returns>
        public static string EncryptSoftiumSHA256(string encryptStr)
        {
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes("Softium_" + encryptStr);//获得以UTF8编码格式的子节数组
            byte[] resultArray = new SHA256Managed().ComputeHash(toEncryptArray);
            return BitConverter.ToString(resultArray).Replace("-", "").ToUpper();
        }

        #endregion


        #region SHA1加密

        public static string EncryptSHA1(string encryptStr)
        {
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);//获得以UTF8编码格式的子节数组
            byte[] resultArray = new SHA1CryptoServiceProvider().ComputeHash(toEncryptArray);
            return BitConverter.ToString(resultArray).Replace("-", "").ToLower();
        }

        #endregion
    }
}
